/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Admin;

import Registration.*;
import java.io.*;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
 *
 * @author Bharath
 */
public class EditMember extends HttpServlet {
   
   EditMemberDao emd ;
   RegistrationDao rd;
   RegistrationBean rbean;
   private String rcheck;
   private boolean isMultipart;
   private boolean b;

   public EditMember()
   {
   emd = new EditMemberDao();
   rd = new RegistrationDao();
   rbean = new RegistrationBean();
   }
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        try {
            isMultipart = ServletFileUpload.isMultipartContent(request);
            out.println(isMultipart);
            // Create a factory for disk-based file items
            FileItemFactory factory = new DiskFileItemFactory();
            // Create a new file upload handler
            ServletFileUpload upload = new ServletFileUpload(factory);
            // Parse the request
            List /* FileItem */ items = upload.parseRequest(request);
            // Process the uploaded items
            Iterator iter = items.iterator();
            while (iter.hasNext())
            {
                FileItem item = (FileItem) iter.next();

                if (item.isFormField()) {
                    processFormField(item);
                } else {
                    processUploadedFile(item);
                }
            }
            HttpSession hs = request.getSession(true);
            rcheck = (String) hs.getAttribute("view_rcheck");
            System.out.println("rcheck value --- in Editmember.java is "+rcheck);
            rbean.setMemberid(rcheck);
            b = rd.update_member(rbean);
            if (b == true)
            {
                System.out.println("Inside If condition EditMembers.java"+b);
                getServletContext().getRequestDispatcher("/EditSuccess.jsp").forward(request, response);
            }
            else
            {
                //System.out.println("Inside else condition EditMembers.java"+b);
                out.println("<script language='javascript'>alert('Edit failed'); location='EditMembers.jsp';</script>");
            }

        } catch(Exception e)
        {
            e.printStackTrace();
        }
   } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
public void processFormField(FileItem item) {
        if (item.isFormField())
        {
            String name = item.getFieldName();
            String value = item.getString();
            
            if (name.equals("name")) {
                System.out.println("name="+name);
                System.out.println("Value"+value);
                rbean.setName(value);                
            }
            if (name.equals("surname")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setSurname(value);
            }
            if (name.equals("sex")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setSex(value);
            }
            if (name.equals("caste"))
            {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setCaste(value);
            }
            if (name.equals("dob"))
            {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setDob(value);
            }
            if (name.equals("age")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setAge(value);
            }
            if (name.equals("qual")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setQualification(value);
            }
            if (name.equals("prof")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setProf(value);
            }
            if (name.equals("org")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setOrg(value);
            }
            if (name.equals("resadd")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setRes_add(value);
            }
            if (name.equals("offadd")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setOff_add(value);
            }
            if (name.equals("state")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setState(value);
            }
            if (name.equals("district")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setDist(value);
            }
            if (name.equals("mandal")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setMandal(value);
            }
            if (name.equals("rphone")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setRes_phone(value);
            }
            if (name.equals("ophone")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setOff_phone(value);
            }
            if (name.equals("mobile")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setMobile(value);
            }
            if (name.equals("email")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setEmail(value);
            }
            if (name.equals("webadd")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setWebadd(value);
            }
            if (name.equals("spouse_name")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setSpouse_name(value);
            }
            if (name.equals("ssex")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setSsex(value);
            }
            if (name.equals("sdob")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setSdob(value);
            }
            if (name.equals("marday")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setMar_aniv(value);
            }
            if (name.equals("squal")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setSqual(value);
            }
            if (name.equals("sprof")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setSprof(value);
            }
            if (name.equals("sorg")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setSorg(value);
            }
            if (name.equals("c1name")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setC1name(value);
            }
            if (name.equals("c1sex")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setC1sex(value);
            }
            if (name.equals("c1dob")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setC1dob(value);
            }
            if (name.equals("c1qual")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setC1qual(value);
            }
            if (name.equals("c2name")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setC2name(value);
            }
            if (name.equals("c2sex")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setC2sex(value);
            }
            if (name.equals("c2dob")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setC2dob(value);
            }
            if (name.equals("c2qual")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setC2qual(value);
            }
            if (name.equals("c3name")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setC3name(value);
            }
            if (name.equals("c3sex")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setC3sex(value);
            }
            if (name.equals("c3dob")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setC3dob(value);
            }
            if (name.equals("c3qual")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setC3qual(value);
            }
            if (name.equals("reffmemid")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
            rbean.setReffmemberid(value);
            }
        }
    }

    private void processUploadedFile(FileItem item) {
        if (!item.isFormField()) {
            try {
                System.out.println("---File Item-------->" + item.getSize());
                if (item.getSize() > 0)
                {
                    rbean.setFile(item.getInputStream());
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

}
